<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>游戏平台 - 登录</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <script>tailwind.config = {theme: {extend: {colors: {primary: '#3B82F6'}}}}</script>
  <style type="text/tailwindcss">@layer utilities { .content-auto { content-visibility: auto; } }</style>
</head>
<body class="bg-gradient-to-br from-blue-50 to-indigo-50 min-h-screen font-sans">
<!-- 导航栏 -->
<nav class="bg-white shadow-md sticky top-0 z-50">
  <div class="container mx-auto px-4 py-3 flex justify-between items-center">
    <a href="/webapp/static" class="text-2xl font-bold text-primary flex items-center"><i class="fa fa-gamepad mr-2"></i>GamePlatform</a>
    <div class="hidden md:flex space-x-6 items-center">
      <a href="/game.html" class="text-gray-700 hover:text-primary transition-colors">游戏中心</a>
      <a href="/scoreboard.html" class="text-gray-700 hover:text-primary transition-colors">排行榜</a>
      <a href="/register.html" class="px-4 py-2 bg-primary text-white rounded-md hover:shadow-lg hover:-translate-y-0.5 transition-all">注册</a>
    </div>
    <button class="md:hidden text-gray-700" id="mobileMenuBtn"><i class="fa fa-bars text-xl"></i></button>
  </div>
  <div id="mobileMenu" class="hidden md:hidden bg-white border-t"><div class="container mx-auto px-4 py-3 flex flex-col space-y-3">
    <a href="/game.html" class="text-gray-700 hover:text-primary py-2">游戏中心</a>
    <a href="/scoreboard.html" class="text-gray-700 hover:text-primary py-2">排行榜</a>
    <a href="/register.html" class="px-4 py-2 bg-primary text-white rounded-md hover:shadow-lg transition-all text-center">注册</a>
  </div></div>
</nav>

<!-- 主内容 -->
<main class="container mx-auto px-4 py-12">
  <div class="max-w-2xl mx-auto">
    <div class="text-center mb-12">
      <p class="text-lg text-gray-600">登录你的账户</p>
    </div>

    <div class="bg-white rounded-xl shadow-md p-8 max-w-md mx-auto">
      <form id="loginForm" class="space-y-6">
        <div>
          <label for="username" class="block text-sm font-medium text-gray-700 mb-1">用户名</label>
          <div class="relative">
            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
              <i class="fa fa-user text-gray-400"></i>
            </div>
            <input type="text" id="username" name="username" class="block w-full pl-10 pr-3 py-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors" placeholder="请输入用户名" required>
          </div>
        </div>

        <div>
          <label for="password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
          <div class="relative">
            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
              <i class="fa fa-lock text-gray-400"></i>
            </div>
            <input type="password" id="password" name="password" class="block w-full pl-10 pr-10 py-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors" placeholder="请输入密码" required>
            <button type="button" id="showPassword" class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-400 hover:text-gray-600">
              <i class="fa fa-eye-slash"></i>
            </button>
          </div>
        </div>

        <div class="flex items-center justify-between">
          <div class="flex items-center">
            <input id="rememberMe" name="rememberMe" type="checkbox" class="h-4 w-4 text-primary focus:ring-primary/50 border-gray-300 rounded">
            <label for="rememberMe" class="ml-2 block text-sm text-gray-700">记住我</label>
          </div>
          <a href="#" class="text-sm text-primary hover:underline">忘记密码？</a>
        </div>

        <div>
          <button type="submit" class="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary transition-all duration-300 hover:shadow-lg hover:-translate-y-0.5">
            登录
          </button>
        </div>
      </form>

      <div class="mt-6 text-center">
        <p class="text-sm text-gray-600">
          还没有账户？ <a href="/register.html" class="font-medium text-primary hover:underline">立即注册</a>
        </p>
      </div>
    </div>
  </div>
</main>

<!-- 通知组件 -->
<div id="toast" class="fixed top-20 right-4 max-w-xs bg-white rounded-lg shadow-lg p-4 transform transition-all duration-300 translate-x-full z-50"><div class="flex items-start">
  <div class="mr-3 mt-1"><i class="fa fa-info-circle text-primary text-lg"></i></div>
  <div class="flex-1"><h4 id="toastTitle" class="font-bold text-gray-800">通知</h4>
    <p id="toastMsg" class="text-gray-600 text-sm mt-1">内容将显示在这里</p>
  </div>
  <button id="closeToast" class="text-gray-400 hover:text-gray-600"><i class="fa fa-times"></i></button>
</div></div>

<script>
  // 显示通知
  function showToast(title, msg, type = 'info') {
    const toast = document.getElementById('toast');
    const titleEl = document.getElementById('toastTitle');
    const msgEl = document.getElementById('toastMsg');
    const icon = toast.querySelector('i');

    titleEl.textContent = title;
    msgEl.textContent = msg;

    // 设置图标和颜色
    icon.className = 'mr-3 mt-1 fa';
    if (type === 'success') {
      icon.classList.add('fa-check-circle', 'text-green-500');
    } else if (type === 'error') {
      icon.classList.add('fa-exclamation-circle', 'text-red-500');
    } else {
      icon.classList.add('fa-info-circle', 'text-primary');
    }

    // 显示通知
    toast.classList.remove('translate-x-full');
    toast.classList.add('translate-x-0');

    // 自动关闭
    setTimeout(() => {
      toast.classList.remove('translate-x-0');
      toast.classList.add('translate-x-full');
    }, 3000);
  }

  // 移动端菜单切换
  document.getElementById('mobileMenuBtn').addEventListener('click', () => {
    document.getElementById('mobileMenu').classList.toggle('hidden');
  });

  // 显示/隐藏密码
  document.getElementById('showPassword').addEventListener('click', () => {
    const passwordInput = document.getElementById('password');
    const icon = document.getElementById('showPassword').querySelector('i');

    if (passwordInput.type === 'password') {
      passwordInput.type = 'text';
      icon.classList.remove('fa-eye-slash');
      icon.classList.add('fa-eye');
    } else {
      passwordInput.type = 'password';
      icon.classList.remove('fa-eye');
      icon.classList.add('fa-eye-slash');
    }
  });

  // 登录表单提交
  document.getElementById('loginForm').addEventListener('submit', (e) => {
    e.preventDefault();
    const username = document.getElementById('username').value.trim();
    const password = document.getElementById('password').value.trim();

    if (!username || !password) {
      showToast('输入错误', '用户名和密码不能为空', 'error');
      return;
    }

    // 显示加载状态
    const submitBtn = document.querySelector('button[type="submit"]');
    const originalText = submitBtn.innerHTML;
    submitBtn.disabled = true;
    submitBtn.innerHTML = '<i class="fa fa-spinner fa-spin mr-2"></i> 登录中...';

    // 模拟API请求
    fetch('/api/auth/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ username, password })
    })
            .then(res => {
              submitBtn.disabled = false;
              submitBtn.innerHTML = originalText;

              if (res.ok) {
                return res.json();
              } else if (res.status === 401) {
                throw new Error('用户名或密码错误');
              } else {
                throw new Error('服务器错误，请稍后再试');
              }
            })
            .then(data => {
              showToast('登录成功', `欢迎回来，${data.username}`, 'success');
              setTimeout(() => {
                window.location.href = '..';
              }, 1500);
            })
            .catch(err => {
              showToast('登录失败', err.message, 'error');
            });
  });

  // 页面加载完成后执行
  document.addEventListener('DOMContentLoaded', () => {
    // 自动聚焦到用户名输入框
    document.getElementById('username').focus();
  });
</script>
</body>
</html>